package cn.b.sky.user.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.enums.DwzCallbackType;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.service.OrganizationService;
import cn.b.sky.user.service.UserService;
import cn.b.sky.user.service.model.Organization;
import cn.b.sky.user.service.model.UserInfo;
import cn.b.sky.user.service.model.UserOrganization;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * <strong>Title : OrganizationController.java<br>
 * </strong> <strong>Package : cn.b.sky.user.controller<br>
 * </strong> <strong>Description : </strong>组织结构管理控制器<br>
 * <strong>Create on : 2015-8-7 下午01:23:23<br>
 * </strong>
 * <p>
 * 
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/org/*")
public class OrganizationController extends SecurityController {
  @Resource
  OrganizationService organizationService;

  @Resource
  UserService userService;

  @RequestMapping("orgList")
  public String menuList(Model model, String navTabId, String selectNode) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("selectNode", selectNode);
    List<Organization> orgs = organizationService.selectOrganizationList(SecurityContextUtil.getSiteCode(), true);
    model.addAttribute("treeList", orgs);
    return "org/org_list";
  }

  @RequestMapping("addOrg")
  public String addOrg(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    if (!StringUtils.isBlank(id)) {
      Organization parent = organizationService.selectOrganization(SecurityContextUtil.getSiteCode(), id);
      Organization org = new Organization();
      org.setParentOrganizationCode(parent.getOrganizationCode());
      org.setParentOrganizationText(parent.getOrganizationText());
      model.addAttribute("org", org);
    }
    return "org/org_add";
  }

  @RequestMapping("validateOrgCode")
  @ResponseBody
  public boolean validateOrgCode(String organizationCode, Model model) {
    boolean isValid = true;
    if (!StringUtils.isBlank(organizationCode)) {
      Organization org = organizationService.selectOrganization(SecurityContextUtil.getSiteCode(), organizationCode);
      if (org != null) {
        isValid = false;
      }
    }
    return isValid;

  }

  @RequestMapping("selectUser")
  public String selectUser(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    List<UserOrganization> users = userService.selectUserTree(SecurityContextUtil.getSiteCode());
    model.addAttribute("treeList", users);
    return "org/user_select";
  }

  @RequestMapping("saveOrg")
  @ResponseBody
  public DWZResult saveOrg(Model model, String navTabId, String id, Organization org, String user,
                           HttpServletRequest request) {
    if (org == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    // 处理组织信息
    org.setSiteCode(SecurityContextUtil.getSiteCode());
    if (org.getLockFlag() == null) {
      org.setLockFlag(0);
    }
    List<UserOrganization> userList = new ArrayList<UserOrganization>();
    // 处理用户
    if (!StringUtils.isEmpty(user)) {
      String[] users = user.split(",");
      for (String s : users) {
        UserOrganization o = new UserOrganization();
        o.setSiteCode(SecurityContextUtil.getSiteCode());
        o.setUserCode(s);
        o.setOrganizationCode(org.getOrganizationCode());
        setCreateInfo(o);
        userList.add(o);
      }

    }
    setCreateInfo(org);
    if (StringUtils.isEmpty(id)) {
      organizationService.insertOrganization(org);
      userService.insertUserOrganization(userList);
    } else {
      organizationService.updateOrganization(org);
      userService.deleteUserOrganizationByOrg(org.getSiteCode(), org.getOrganizationCode());
      userService.insertUserOrganization(userList);
    }
    String forwardUrl = getForwardUrl(request) + "/org/orgList?navTabId=" + navTabId + "&selectNode="
        + org.getOrganizationCode();
    return navTabAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, DwzCallbackType.FORWARD, forwardUrl, null, null);

  }

  @RequestMapping("editOrg")
  public String editOrg(Model model, String navTabId, String id) {
    Organization org = organizationService.selectOrganization(SecurityContextUtil.getSiteCode(), id);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("org", org);
    if (org != null) {
      List<UserInfo> users = userService.selectUserByOrganization(SecurityContextUtil.getSiteCode(),
          org.getOrganizationCode());

      model.addAttribute("users", users);
    }
    return "org/org_add";
  }

  @RequestMapping("deleteOrg")
  @ResponseBody
  public DWZResult deleteOrg(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isEmpty(id)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    List<Organization> mlist = organizationService.selectOrgByParent(SecurityContextUtil.getSiteCode(), id, true);
    if (mlist != null && mlist.size() > 0) {
      return ajaxError(
          getImeViewMessageI18n(null, "cannotDelOrg.M"));
    }
    userService.deleteUserOrganizationByOrg(SecurityContextUtil.getSiteCode(), id);
    organizationService.deleteOrganization(SecurityContextUtil.getSiteCode(), id);
    return navTabAjaxDone(navTabId, null, null, null,
        getImeViewMessageI18n(null, "successfulOperation.M"),
        null);

  }

  @RequestMapping("viewOrg")
  public String viewOrg(Model model, String navTabId, String id) {
    Organization org = organizationService.selectOrganization(SecurityContextUtil.getSiteCode(), id);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("org", org);
    if (org != null) {
      List<UserInfo> users = userService.selectUserByOrganization(SecurityContextUtil.getSiteCode(),
          org.getOrganizationCode());

      model.addAttribute("users", users);
    }
    return "org/org_view";

  }

  @RequestMapping({ "flowNodeOrgLookUp" })
  public String flowNodeOrgLookUp(Model model) {
    List<Organization> orgs = this.organizationService.selectOrganizationList(SecurityContextUtil.getSiteCode(), false);

    model.addAttribute("treeList", orgs);
    return "engine/flownode/flowNodeOrgLookUp";
  }

}
